package com.program.houai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.program.houai.entity.GenerateOrderNumberEntity;
import com.program.houai.exception.BusinessException;
import com.program.houai.exception.ErrorType;
import com.program.houai.mapper.GenerateOrderNumberMapper;
import com.program.houai.service.IGenerateOrderNumberService;
import com.program.houai.util.DateFullUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @version v1.0
 * @ProjectName: EOrder-master
 * @ClassName: GenerateOrderNumberServiceImpl
 * @Description: TODO(一句话描述该类的功能)
 * @Author: Topcheer
 * @Date: 2024/8/31 9:10
 */
@Service
public class GenerateOrderNumberServiceImpl extends ServiceImpl<GenerateOrderNumberMapper, GenerateOrderNumberEntity> implements IGenerateOrderNumberService {


    @Override
    public String generateOrderNumber() {

        GenerateOrderNumberEntity generateOrderNumberEntity =    this.baseMapper.selectById(1);

        String curryDay = DateFullUtils.getDateStr(new Date(),DateFullUtils.YYYYMMDD);

        int version =  generateOrderNumberEntity.getVersion();

        int nextVersion = version+1;

        String orderNumber = curryDay + String.format("%07d", nextVersion);

        int result = this.baseMapper.updateOrderNumberByVserion(generateOrderNumberEntity.getId(),version,nextVersion,orderNumber);
        if (result > 0 ){

        }else{
            throw new BusinessException(ErrorType.BIZ_ERROR,"操作频繁稍后再试！");
        }

        return orderNumber;
    }
}
